﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Net.Sockets;

namespace LLL
{
    public class EmailSender
    {
        /// <summary>
        /// 用来发送Email的地址
        /// </summary>
        private string EmailServer { get; set; }
        /// <summary>
        /// 用来发送Email的帐号
        /// </summary>
        private string EmailUid { get; set; }
        /// <summary>
        /// 用来发送Email的帐号密码
        /// </summary>
        private string EmailPwd { get; set; }
        /// <summary>
        /// 用来显示的Email地址
        /// </summary>
        private string SendEmailAddress { get; set; }

        /// <summary>
        /// 异常信息
        /// </summary>
        private string ErrString { get; set; }

        /// <summary>
        /// 是否使用html
        /// </summary>
        private bool isHtml;

        /// <summary>
        /// 构造
        /// </summary>
        public EmailSender() { }

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="server"></param>
        /// <param name="uid"></param>
        /// <param name="pwd"></param>
        /// <param name="sendAddress"></param>
        public EmailSender(string server ,string uid ,string pwd, string sendAddress ,bool isHtml) {
            this.EmailPwd = pwd;
            this.EmailServer = server;
            this.EmailUid = uid;
            this.SendEmailAddress = sendAddress;
            this.isHtml = isHtml;
        }

        /// <summary>
        /// 发送Email
        /// </summary>
        /// <param name="EmailBody"></param>
        /// <returns></returns>
        public bool SendEmail(string EmailTitle ,string EmailBody ,string TargetEmailAddr)
        {
            bool outBool = false;
            try
            {
                MailAddress from = new MailAddress(this.SendEmailAddress);
                MailAddress to = new MailAddress(TargetEmailAddr);
                MailMessage mailobj = new MailMessage(from, to);
                mailobj.Subject = this.funcEncode(EmailTitle);

                mailobj.Body = EmailBody;
                mailobj.IsBodyHtml = this.isHtml;
                mailobj.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
                mailobj.Priority = MailPriority.High;
                //如果有附件：
                /*
                 if(aFileUpload.PostedFile.ContentLength > 0)
                 {
                  mailobj.Attachments.Add(new Attachment(aFileUpload.PostedFile.FileName));
                 }
                 * */
                //根据自己的邮箱，选择发邮件的SMTP：
                SmtpClient smtp = new SmtpClient(this.EmailServer);
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new NetworkCredential(this.EmailUid, this.EmailPwd);
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.Send(mailobj);
                outBool = true;
            }
            catch (Exception err)
            {
                ErrString = err.Message;
                outBool = false;
            }
            return outBool;
        }

        /// <summary>
        /// 获得错误信息
        /// </summary>
        /// <returns></returns>
        public string getErrorMessage() {
            return this.ErrString;
        }

        /// <summary>
        /// 编码用
        /// </summary>
        /// <param name="aStr"></param>
        /// <returns></returns>
        private string funcEncode(string aStr)
        {
            string c = "";
            aStr = System.Web.HttpUtility.UrlEncode(aStr, System.Text.Encoding.GetEncoding("GB2312"));
            int j = 0;
            for (int i = 0; i < aStr.Length; i++)
            {
                if (aStr[i] == '%')
                {
                    c += "=";
                    j = i;
                }
                if (i - j < 3 && i - j > 0 && j != 0)
                {
                    c += aStr[i].ToString().ToUpper();
                }
                else
                {
                    if (aStr[i] != '%')
                        c += aStr[i];
                }

            }

            //为了接收邮件不出现乱码，我加了一个标头，声明用GB2312转换而成的QP编码。
            return "=?GB2312?Q?" + c + "?=";
        }
    }
}
